package com.raymond.smpp.enums;

import com.raymond.client.codec.decoding.BaseDecoder;
import com.raymond.client.codec.encodeing.BaseEncoder;
import com.raymond.smgp.codec.decoding.*;
import com.raymond.smgp.codec.encodeing.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @version V3.0
 * @description: smgp的各种命令类型
 * @author: ywj
 * @date: 2020年06月08日10:58
 **/
public enum CommandIdType {
    /**
     * 连接请求
     */
    CONNECT_REQUEST(0x00000001, null, ConnectRequestMessageEncoder.class),
    /**
     * 连接相应
     */
    CONNECT_RESPONSE(0x80000001, ConnectResponseMessageDecoder.class, null),

    /**
     * 提交短信
     */
    SUBMIT_REQUEST(0x00000002, null, SubmitRequestMessageEncoder.class),
    /**
     * 提交短信响应
     */
    SUBMIT_RESPONSE(0x80000002, SubmitResponseMessageDecoder.class, null),
    /**
     * 回执请求
     */
    DELIVER_REQUEST(0x00000003, DeliverRequestMessageDecoder.class, null),
    /**
     * 回执响应
     */
    DELIVER_RESPONSE(0x80000003, null, DeliverResponseMessageEncoder.class),
    /**
     * 心跳检测
     */
    ACTIVE_TEST_REQUEST(0x00000004, ActiveTestRequestMessageDecoder.class, ActiveTestRequestMessageEncoder.class),
    /**
     * 心跳检测响应
     */
    ACTIVE_TEST_RESPONSE(0x80000004, ActiveTestResponseMessageDecoder.class, ActiveTestResponseMessageEncoder.class),

    /**
     * 断开检测
     */
    TERMINATE_REQUEST(0x00000006, null, TerminateRequestMessageEncoder.class),
    /**
     * 断开检测响应
     */
    TERMINATE_RESPONSE(0x80000006, TerminateResponseMessageDecoder.class, null);

    private static final Logger logger = LoggerFactory.getLogger(CommandIdType.class);

    private int commandId;
    private Class<? extends BaseDecoder> decoder;

    private Class<? extends BaseEncoder> encoder;

    private CommandIdType(int commandId, Class<? extends BaseDecoder> decoder, Class<? extends BaseEncoder> encoder) {
        this.commandId = commandId;
        this.decoder = decoder;
        this.encoder = encoder;
    }


    public int getCommandId() {
        return commandId;
    }


    public BaseDecoder getDecoder() {
        try {
            if (decoder == null) {
                return null;
            }
            return decoder.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    public BaseEncoder getEncoder() {
        try {
            if (encoder == null) {
                return null;
            }
            return encoder.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            logger.error(e.getMessage());
        }
        return null;
    }
}
